#!/bin/sh

export DEBIAN_PRIORITY=critical


getnewbundle=$1
if [ "x$getnewbundle" != "x" ] ; then
        filetoget=`wget -O - http://smartcard20.com/staticdownload/install-media-bundle-and-images/install-media-bundle-public-sc20-latest.txt`
        wget http://han.pp.se/static/sc20/${filetoget}
        tar jxvf ${filetoget}

        installmediadundledirname=`find . -type d -name 'install-media-bundle-*'`

        if [ "x$installmediadundledirname" == "x" ] ; then
                echo "could not find the install-media-bundle- dir"
                exit
        else
                cd ${installmediadundledirname}
        fi
else

        inimbdir=`pwd | grep install-media-bundle-`

        if [ "x$inimbdir" == "x" ] ; then
                echo "you need to execute $0 while standing in the install-media-bundle directory"
                exit
        else
                cd ${inimbdir}
        fi
fi

apt-get -y install screen sudo bzip2 unzip mysql-server

apt-get -y purge nfs-common portmap

perl -pi -e 's/127\.0\.0\.1.*localhost/127.0.0.1     localhost.localdomain localhost smartcard20.demo smartcard20 /gio' /etc/hosts
perl -pi -e 's/127\.0\.1\.1.*//gio' /etc/hosts

perl -pi -e 's/debian/smartcard20.demo/gio' /etc/hostname
hostname smartcard20.demo

useradd --create-home --shell /bin/bash --password YG30h0h7gzH5M sc20

./unpack-media-bundle-files.sh

mkdir -p /var/cache/jboss4/tmp
chown jboss /var/cache/jboss4/tmp
mkdir -p /var/log/jboss4/
chown jboss /var/log/jboss4/
mkdir -p /var/run/jboss4/
chown jboss /var/run/jboss4/

unzip fw2.zip
mv fw2 /etc
myIF=$(/sbin/ifconfig | egrep "^eth" | awk '{print $1}')
/etc/fw2/add-firewall-to-debian-interfaces.pl $myIF

echo "sc20    ALL=(ALL) ALL" >> /etc/sudoers

mkdir /usr/local/www
cp iidsetup.exe /usr/local/www
cp jre*exe /usr/local/www

echo "Final steps needs manual labor, sorry :-)"
echo "1. reboot image/machine so all services gets the new hostname"
echo "2. login again and run htmf-setup"
echo "3. run /usr/local/htmf/src/inst/smartcard20/configure-apache.sh"
echo "4. shutdown the machine"
echo "5. make a copy of the vmware-image"
echo "6. start image again and verify operations by issuing ISA and a few test cards"



